package publish

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/api/internal/svc"
	"group-card/api/internal/types"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"
)

type PublishCreateCardNumberLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPublishCreateCardNumberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishCreateCardNumberLogic {
	return &PublishCreateCardNumberLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

//region TODO: 自动生成卡号 Author:LiuYanLin Date:2023/04/04 周二 11:12

func (l *PublishCreateCardNumberLogic) PublishCreateCardNumber(req *types.CreateCardNumberPublishRequest) (resp *types.PublishResponse, err error) {

	if req.Count < 1 {
		return &types.PublishResponse{Code: response.ErrCode, Message: "至少输入一张"}, nil
	}

	data, err := l.svcCtx.Card.PublishCreateCardNumber(l.ctx, &cardclient.PublishCreateCardNumberRequest{
		Count: req.Count,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("自动生成卡号失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.PublishResponse{Code: response.ErrCode, Message: "自动生成卡号失败"}, nil
	}

	return &types.PublishResponse{
		Code:    response.SuccessCode,
		Message: "自动生成卡号成功",
		Data:    data,
	}, nil

}

//endregion
